package com.mylib.libcore.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.mylib.libcore.bean.EveryDayCoin;
import com.mylib.libcore.bean.EveryDayConfigBean;

import static com.mylib.libcore.config.AppSPKeyConfig.APP_FUNCTION_TABS;
import static com.mylib.libcore.config.AppSPKeyConfig.APP_OA_ID;
import static com.mylib.libcore.config.AppSPKeyConfig.APP_SYS_WIDTH;
import static com.mylib.libcore.config.AppSPKeyConfig.HOME_TAB_JSON;
import static com.mylib.libcore.config.AppSPKeyConfig.IS_OPEN_AD;
import static com.mylib.libcore.config.AppSPKeyConfig.NEWS_KEY;
import static com.mylib.libcore.config.AppSPKeyConfig.NEWS_ZX_KEY_ID;
import static com.mylib.libcore.config.AppSPKeyConfig.TODAY_LOTTERY_NUM;

public class AppPreferenceForever {

    private String prefFileName;
    private final SharedPreferences mPrefs;
    private final Gson mGson;

    public AppPreferenceForever(Context context) {
        mGson = JsonUtil.gsonBuilder.create();
        prefFileName = context.getPackageName() + "_forever";
        mPrefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
    }

    //清空缓存
    public void clearPreferencesForever(){
        mPrefs.edit().clear().apply();
    }

    //缓存关键字
    public void setKeyValue(String key,String value){
        mPrefs.edit().putString(key, value).apply();
    }
    //获取关键字
    public String getKeyValue(String key){
        return mPrefs.getString(key, "")+"";
    }
    //获取开放table
    public String getMainTabs(){
        return mPrefs.getString(APP_FUNCTION_TABS, "")+"";
    }
    //设置开放table
    public void setMainTabs(String value){
        mPrefs.edit().putString(APP_FUNCTION_TABS, value).apply();
    }


    //缓存news关键字
    public void setNewsKey(String value){
        mPrefs.edit().putString(NEWS_KEY, value).apply();
    }
    //获取news关键字
    public String getNewsKey(){
        String tk = mPrefs.getString(NEWS_KEY, "4426eebb12da7fc1bac6a538328f6264");
        if(UIUtils.isEmpty(tk)) return "4426eebb12da7fc1bac6a538328f6264";
        else return tk;
    }

    //缓存news关键字
    public void setNewsDetailsKeyID(String value){
        mPrefs.edit().putString(NEWS_ZX_KEY_ID, value).apply();
    }
    //获取news关键字
    public String getNewsDetailsKeyID(){
        String tk = mPrefs.getString(NEWS_ZX_KEY_ID, "js_article_content;article-content;post_body");
        if(UIUtils.isEmpty(tk)) return "";
        else return tk;
    }

    //获取getSelectTabs
    public String getSelectTabs(){
        return mPrefs.getString(HOME_TAB_JSON, "")+"";
    }
    //设置setSelectTabs
    public void setSelectTabs(String value){
        mPrefs.edit().putString(HOME_TAB_JSON, value).apply();
    }

    //获取广告开关
    public String getADOpen(){
        return mPrefs.getString(IS_OPEN_AD, "")+"";
    }
    //设置广告开关
    public void setADOpen(String value){
        mPrefs.edit().putString(IS_OPEN_AD, value).apply();
    }

    //获取oaid
    public String getAppOAID(){
        return mPrefs.getString(APP_OA_ID, "")+"";
    }
    //设置oaid
    public void SetAppOAID(String value){
        mPrefs.edit().putString(APP_OA_ID, value).apply();
    }

    //获取getAppSysWidth
    public Integer getAppSysWidth(){
        return mPrefs.getInt(APP_SYS_WIDTH, 360);
    }
    //设置getAppSysWidth
    public void setAppSysWidth(int value){
        mPrefs.edit().putInt(APP_SYS_WIDTH, value).apply();
    }

    //获取今日抽奖次数
    public int getTodayLotteryNum(){
        return mPrefs.getInt(TODAY_LOTTERY_NUM, 0);
    }
    //设置今日抽奖次数
    public void setTodayLotteryNum(int value){
        mPrefs.edit().putInt(TODAY_LOTTERY_NUM, value).apply();
    }

    //设置用户每日金币领取记录
    public void setEveryDayCoin(EveryDayCoin bean){
        if(UIUtils.isEmpty(bean)) return;
        mPrefs.edit().putString("every_day_coin", JsonUtil.toJson(bean)).apply();
    }
    //获取用户每日配置
    public EveryDayCoin getEveryDayCoin(){
        String every_day_coin = mPrefs.getString("every_day_coin", "");
        if(!UIUtils.isEmpty(every_day_coin)){
            EveryDayCoin bean = (EveryDayCoin)JsonUtil.parseJsonToBean(every_day_coin,EveryDayCoin.class);
            if(!UIUtils.isEmpty(bean))  return bean;
                else return  getNewDayCoin();
        }else return  getNewDayCoin();
    }

    private EveryDayCoin  getNewDayCoin(){
        EveryDayCoin coin = new EveryDayCoin();
        coin.setLogin_coin(0);
        coin.setOnline_coin(0);
        coin.setDk_coin(0);
        coin.setSign_coin(0);
        coin.setLl_coin(0);
        coin.setGame_coin(0);
        coin.setNovel_coin(0);
        coin.setTz_coin(0);
        coin.setCg_coin(0);
        coin.setNews_coin(0);
        coin.setWeather_coin(0);
        coin.setVideo_coin(0);
        coin.setBk_coin(0);
        coin.setInvite_coin(0);
        coin.setZs_coin(0);
        coin.setLottery_coin(0);
        coin.setWw_coin(0);
        coin.setRank_coin(0);
        return coin;
    }
}
